// require("ocr.js")
// require("utils.js")
logWindow.close()

function commonClose通用X关闭() {
    var ocr1x = opencv.OCREx('图色251873.cv');
    if (ocr1x != null) {
        agentOrHidClick(ocr1x.getAllDetect()[0])
    }
}

function get获取宝箱() {
    log('get获取宝箱')
    if (hasExcuteFunc('get获取宝箱')) {
        return
    }

    var find宝箱
    var detects宝箱 = opencv.findImagesEx('图色251957.cv');
    if (detects宝箱 == null) {
        detects宝箱 = opencv.findMultiColorEx('图色732822.cv');
    }

    if (detects宝箱 != null) {
        find宝箱 = detects宝箱[0]
    } else {
        find宝箱 = ocrText('领金币')
    }

    agentOrHidClick(find宝箱, 2, () => {

        agentOrHidClick(ocrWaitForText('开宝箱'))

        if (ocrText('明日再来')) {
            setExcuteFunc('get获取宝箱')
        }
        //x
        var detects = opencv.findImagesEx('图色248908.cv');
        if (detects != null) {
            agentOrHidClick(detects[0])
        }

        if (ocrText('抢幸运', '大宝箱', '开宝箱', '抽奖', '做任务领奖励')) {
            agentOrHidback()
        }
    })


}


function isIn直播界面_sample() {
    log('检测是否在直播间内')
    var flag直播主界面 = null
    //找色返回目标数组
    var detects送礼物 = opencv.findMultiColorEx('图色162528.cv');
    if (detects送礼物 != null) {
        flag直播主界面 = detects送礼物[0]
        log('检测到送礼物')
    }

    //找色返回目标数组
    var detects爱心 = opencv.findMultiColorEx('图色940531.cv');
    if (detects爱心 != null) {
        flag直播主界面 = detects爱心[0]
        log('检测到送爱心')
    }

    // if (flag直播主界面 == null) {
    //     //购物车
    //     var detectsggc购物车 = opencv.findMultiColorEx('图色536787.cv');
    //     if (detectsggc购物车 != null) {
    //         flag直播主界面 = detectsggc购物车[0]
    //     }
    // }

    if (flag直播主界面 == null) {
        // ocrText('说点什么', true)

        flag直播主界面 = ocrText('更多直播', '说点什么', '更多关注', '小时榜', '专属')
        if (flag直播主界面) {
            log('检测到说点什么')
        }
    }

    // //找色返回目标数组
    // var detects = opencv.findMultiColorEx('图色759083.cv');
    // if (detects != null) {
    //     printl(detects);
    //     detects[0].click();
    // }

    // if (flag直播主界面 == null) {
    //     flag直播主界面 = ocrText('说点什么')
    // }

    return flag直播主界面 != null
}

// SystemPrint.log(isIn直播界面())
function isIn直播界面(is只判断下半屏) {
    if (is只判断下半屏 == undefined) is只判断下半屏 = false
    var flag直播主界面 = null
    if (currentAppPackName == 'com.phoenix.read') {
        if (ocrText('新人福利', '日常福利', '联系方式', '去提现', '看视频赚')) {
            return false
        }
    }
    //找色返回目标数组
    var detects送礼物 = opencv.findMultiColorEx('图色162528.cv');
    if (detects送礼物 != null) {
        flag直播主界面 = detects送礼物[0]
        SystemPrint.log('1')
    }


    if (flag直播主界面 == null) {
        //购物车
        var detectsggc购物车 = opencv.findMultiColorEx('图色536787.cv');
        if (detectsggc购物车 != null) {
            flag直播主界面 = detectsggc购物车[0]
            SystemPrint.log('2')
        }
    }

    // if (flag直播主界面 == null) {
    //     var detects福袋 = opencv.findMultiColorEx('图色233817.cv');
    //     if (detects福袋 != null) {
    //         flag直播主界面 = detects福袋[0]
    //     }
    // }

    // if (flag直播主界面 == null) {
    //     //找色返回目标数组
    //     var detects优惠券 = opencv.findMultiColorEx('图色119098.cv');
    //     if (detects优惠券 != null) {
    //         flag直播主界面 = detects优惠券[0]
    //     }
    // }

    if (flag直播主界面 == null) {
        //找色返回目标数组
        var detects宝箱 = opencv.findMultiColorEx('图色780379.cv');
        if (detects宝箱 == null) {
            detects宝箱 = opencv.findMultiColorEx('图色732822.cv');
        }
        if (detects宝箱 != null) {
            flag直播主界面 = detects宝箱[0]
            SystemPrint.log('3')
        }
    }

    if (flag直播主界面 == null) {
        //找色返回目标数组
        var detects游戏 = opencv.findMultiColorEx('图色248866.cv');
        if (detects游戏 != null) {
            flag直播主界面 = detects游戏[0]
            SystemPrint.log('4')
        }
    }

    if (!is只判断下半屏) {
        if (flag直播主界面 == null) {
            // ocrText('说点什么', true)
            flag直播主界面 = ocrText('更多直播', '说点什么', '更多关注', '小时榜', '点击进入', '专属', '属礼物')
            SystemPrint.log('5')
        }
    } else {
        if (flag直播主界面 == null) {
            // ocrText('说点什么', true)
            flag直播主界面 = ocrText('说点什么', '点击进入', '专属', '属礼物')
            SystemPrint.log('6')
        }
    }

    // //找色返回目标数组
    // var detects = opencv.findMultiColorEx('图色759083.cv');
    // if (detects != null) {
    //     printl(detects);
    //     detects[0].click();
    // }

    // if (flag直播主界面 == null) {
    //     flag直播主界面 = ocrText('说点什么')
    // }

    return flag直播主界面 != null
}

function back2直播界面(repeattimes) {
    if (repeattimes == undefined) repeattimes = 3
    log('返回直播界面')
    if (ocrText('点击进入', '领取成功', '领取奖励', '打赏')) {
        click(0.5, 0.4, 3)
    }

    backUntilFindView(() => {
        // SystemPrint.log(isIn直播界面())
        return isIn直播界面(true)
    }, repeattimes, 10)


    // if (ocrText('抢幸运', '大宝箱', '开宝箱', '抽奖', '取消关注')) {
    //     // click(0.5, 0.1)
    //     agentOrHidback()
    // }

    // 点击上半屏回到直播界面
    if (ocrText('抢幸运', '大宝箱', '开宝箱', '抽奖')) {
        click(0.01, 0.1)
    }
}

function get福袋奖励等待() {
    whilestep((steptimes, alltimes, cb) => {
        if (steptimes % 6 == 0 || steptimes == 0) {
            get获取宝箱()
        }

        if (steptimes % 12 == 0) {
            if (ocrText('知道了', '下次再说', '我知道')) {
                cb()
            }
        }
        if (steptimes % 36 == 0 || steptimes == 0) {
            // var fordzTimes = getRandomNum(30, 80)
            // for (var i = 0; i < fordzTimes; i++) {
            //     click(0.5, 0.3, 300)
            // }
        }
    }, 20 * 60)
}

function get福袋奖励() {
    // 福袋
    log('领取福袋')
    var detects福袋 = opencv.findImagesEx('图色757451.cv');
    if (detects福袋 == null) {
        detects福袋 = opencv.findMultiColorEx('图色824143.cv');
    }
    if (detects福袋 == null) {
        detects福袋 = opencv.findMultiColorEx('图色233817.cv');
    }
    if (detects福袋 != null) {
        printinfoOld('准备抢福袋')
        addExcuteFuncTimes('直播福袋')
        agentOrHidClick(detects福袋[0], 3)

        agentOrHidClick(ocrWaitForMoreTextMillisecond('去发表评论', '一键发表', '去发表评', '发表评论', '发表评', 10))
        agentOrHidClick(ocrText('去发表评论', '参与抽奖', '参与福袋', '去发'))

        sleep.second(2)
        var sendFlag = ocrWaitForMoreTextMillisecond('发送', '送', '发', 8)
        if (sendFlag == null) {
            //找图返回目标数组
            var detects = opencv.findImagesEx('图色526362.cv');
            if (detects != null) {
                sendFlag = detects[0]
            }
        }


        agentOrHidClick(sendFlag, 2, () => {
            if (ocrText('参与成功')) {
                click(0.5, 0.3, 50)
            }

            // get福袋奖励等待()
        })

        sleep.second(2)
        agentOrHidClick(ocrTextMoreInCenter('知道了', '我知道', '发送'))
        back2直播界面(2)
    }

}

function write说点什么() {
    if (hasExcuteFuncInTime是否在一段时间内执行过('write说点什么')) {
        return
    }

    // SystemPrint.log()
    if (getMaxNumByNearBy(ocrText('关注')) < 5) {
        return
    }

    agentOrHidClick(ocrText('说点什么', '说点什', '点什么'), 2, () => {
        var detects游戏 = opencv.findImagesEx('图色852194.cv');
        if (detects游戏) {
            Clipboard.copy('好玩吗')
        } else {

        }

        var randnum = getRandomNum(0, 100)
        if (randnum > 75) {
            //找图返回目标数组
            var detects = opencv.findImagesEx('图色424667.cv');
            if (detects != null) {
                agentOrHidClick(detects[0])
                agentOrHidClick(detects[0])
                agentOrHidClick(detects[0])
            }
        } else if (randnum > 50) {
            //找图返回目标数组
            var detects = opencv.findImagesEx('图色804287.cv');
            if (detects != null) {
                agentOrHidClick(detects[0])
                agentOrHidClick(detects[0])
                agentOrHidClick(detects[0])
            }
        } else if (randnum > 25) {
            //找图返回目标数组
            var detects = opencv.findImagesEx('图色227522.cv');
            if (detects != null) {
                agentOrHidClick(detects[0])
            }
        } else {
            //找图返回目标数组
            var detects = opencv.findImagesEx('图色245819.cv');
            if (detects != null) {
                agentOrHidClick(detects[0])
                agentOrHidClick(detects[0])
                agentOrHidClick(detects[0])
            }
        }

        agentOrHidClick(ocrWaitForText('发送', '送', '发', 3))
        setExcuteFuncInTime一段时间内不再执行('write说点什么', 20 * 60)
    })

    back2直播界面(1)
}

function enter橱窗() {
    log('进入橱窗')
    if (!hasExcuteFuncInTime是否在一段时间内执行过('go看直播挂机_进入橱窗')) {

        clickAbs(50, 120)
        // var detects = opencv.findMultiColorEx('图色823310.cv');
        // if (detects != null) {
        //     agentOrHidClick(detects[0])


        // }
        var go橱窗 = ocrWaitForMoreTextMillisecondInRegion(0, 0.5, 1, 1, 4, '逛橱窗', '橱窗', '进店')
        agentOrHidClick(go橱窗, 2, () => {
            var times = getRandomNum(1, 6)
            reperatAgentSwipSmallDown循环向下浏览(false, times, 6)
            setExcuteFuncInTime一段时间内不再执行('go看直播挂机_进入橱窗', 20 * 60)
        })

        log('随机进入商品')
        agentOrHidClick(ocrText('全球购'), 3, () => {
            ocrWaitForText('加入购物车')
            var times = getRandomNum(3, 12)
            sleep.second(3)
            reperatAgentSwipSmallDown循环向下浏览(false, times, 6)
        })

        back2直播界面(2)

        agentOrHidClick(ocrText('知道了', '下次再说'))
    }
}

function entershopCar购物车(sleeptime) {
    if (sleeptime == undefined) sleeptime = 10
    log('进入购物车或游戏浏览')
    if (!hasExcuteFuncInTime是否在一段时间内执行过('go看直播挂机_进入购物车浏览商品')) {
        var detects = opencv.findMultiColorEx('图色536787.cv');
        if (detects != null) {
            agentOrHidClick(detects[0])

            // 讲解中
            // agentOrHidClickOffectY(ocrText('讲解中'), 50, 2, () => {
            //     reperatAgentSwipSmallDown循环向下浏览()
            // })

            // 去抢购
            agentOrHidClickAbsoultXOffectY(ocrText('去抢购', '领券抢购'), screen.percentToWidth(0.17), 50, 2, () => {
                reperatAgentSwipSmallDown循环向下浏览()
            })

            back2直播界面()
        }


        agentOrHidClick(ocrText('知道了'))

        setExcuteFuncInTime一段时间内不再执行('go看直播挂机_进入购物车浏览商品', 5 * 60)
    }



    var detects游戏 = opencv.findImagesEx('图色852194.cv');
    if (detects游戏 != null) {
        log('游戏详情浏览')
        agentOrHidClick(detects游戏[0])
        sleep.second(sleeptime)

        whilestep((s, a, canbreak) => {
            agentSwipSmallDown()
            if (ocrText('反馈与', '与投诉', '全部评价')) {
                canbreak()
            }

            if (getRandomNum(0, 100) > 50) {
                agentClickRight(ocrText('展开'), 2, () => {
                    //找图返回目标数组
                    var detects点赞 = opencv.findImagesEx('图色602509.cv');
                    if (detects点赞 != null) {
                        agentOrHidClick(detects点赞[0])
                    }

                    agentOrHidClick(ocrText('有帮助'))
                })
            }


        }, 10, 10)
        // reperatAgentSwipSmallDown循环向下浏览(false, 10, 6)

        back2直播界面()
    }
}

function share分享直播间(random) {
    printinfoOld('分享直播间')
    if (!hasExcuteFunc('go看直播挂机_分享直播') && random > 80) {
        var detects更多 = opencv.findImagesEx('图色511811.cv');
        if (detects更多 != null) {
            agentOrHidClick(detects更多[0])
            agentOrHidClick(ocrWaitForTextMillisecondInRegion(0, 0.5, 1, 1, '分享', 4), 1, () => {
                // agentOrHidClick(ocrWaitForMoreTextMillisecond('图图图', '粥粥吃不饱'))
                expectView(() => {
                    //找图返回目标数组
                    var detects图图图 = opencv.findImagesEx('图色448854.cv');
                    if (detects图图图 != null) {
                        return detects图图图[0]
                    }
                }, () => {
                    var flag私信 = ocrText('私信给', '给朋友')
                    if (flag私信 && flag私信.getRect()) {
                        agentSwipRealPoint(screen.percentToWidth(0.7), flag私信.getRect().getCenterY() + 200, screen.percentToWidth(0.1), flag私信.getRect().getCenterY() + 200)
                    }
                    sleep.second(1)
                }, (findView) => {
                    agentOrHidClick(findView)
                    agentOrHidClick(ocrText('发送'))
                })
                setExcuteFunc('go看直播挂机_分享直播')
            })

            back2直播界面(2)
        }
    }
}

// go看直播详情()
function go看直播详情(sleepSecond) {
    if (sleepSecond == undefined) sleepSecond = 5
    printinfoOld('go看直播详情')
    ocrWaitForText('关注', 3, 1)
    if (ocrText('首页', '任务中心', '日常任务', '看广告', '看视频')) {
        return
    }

    if (ocrText('抢幸运', '大宝箱')) {
        agentOrHidback()
    }

    get福袋奖励()

    write说点什么()

    get获取宝箱()

    back2直播界面()

    // 进行点赞
    if (isIn直播界面()) {
        // var fordzTimes = getRandomNum(50, 60)
        // for (var i = 0; i < fordzTimes; i++) {
        //     click(0.5, 0.3, 300)
        // }
    }

    get获取宝箱()

    back2直播界面()

    // 点击上半屏回到直播界面
    if (ocrText('抢幸运', '大宝箱', '开宝箱', '抽奖', '做任务领奖励')) {
        agentOrHidback()
    }

    agentOrHidClick(ocrText('知道了', '下次再说'))

    sleep.second(sleepSecond)

    enter橱窗()

    // 进入购物车浏览商品
    entershopCar购物车()

    // share分享直播间(Math.random() * 100)

    back2直播界面()
}


// go看直播挂机()
function go看直播挂机() {
    log('看直播挂机')

    var hasChangeView = false

    log('持续等待直到进入直播间')
    if (ocrText('点击进入', '点击', '下单最高', '最高返', '进入直播')) {
        click(0.5, 0.3)
    }
    if (ocrText('再看一个', '再看一次')) {
        agentOrHidback()
    }

    expectView(() => {
        return isIn直播界面()
    }, (stepTimes) => {
        log('等待中....' + stepTimes)
        sleep.second(1)
    }, () => {
        log('成功进入到直播间')
    }, null, 30)

    print.warn('注意直播任务关注, 亲密度, 购物车(浏览+加购), 分享直播间, 评论')
    // log('倒数5秒后开始执行直播任务')
    // sleep.second(5)

    agentOrHidClick(ocrText('暂不开启', '暂不'))

    if (!hasChangeView) {
        click(0.01, 0.1)
        sleep.second(3)
        click(0.01, 0.1)
        sleep.second(3)
        var detectsmore = opencv.findImagesEx('图色549171.cv');
        if (detectsmore == null) {
            detectsmore = opencv.findImagesEx('图色254021.cv');
        }
        if (detectsmore != null) {
            agentOrHidClick(detectsmore[0])
            agentOrHidClick(ocrText('清晰', '晰度'))
            agentOrHidClick(ocrText('标清', '标'))
            hasChangeView = true
        } else {
            click(0.01, 0.1)
        }
    }


    // 领取优惠券
    log('领取优惠券')
    var detects优惠券 = opencv.findMultiColorEx('图色119098.cv');
    if (detects优惠券 != null) {
        agentOrHidClick(detects优惠券[0])

        var ocr1 = opencv.OCREx('图色694693.cv');
        if (ocr1 != null) {
            if (ocr1.getAllString().includes('x')) {
                agentOrHidClick(ocr1.getAllDetect()[0])
            }
        }
    }

    get获取宝箱()

    if (!hasChangeView) {
        click(0.01, 0.1)
        sleep.second(3)
        click(0.01, 0.1)
        sleep.second(3)
        //找图返回目标数组
        var detectsmore = opencv.findImagesEx('图色549171.cv');
        if (detectsmore == null) {
            detectsmore = opencv.findImagesEx('图色254021.cv');
        }
        if (detectsmore != null) {
            agentOrHidClick(detectsmore[0])
            agentOrHidClick(ocrText('清晰', '晰度'))
            agentOrHidClick(ocrText('标清', '标'))
            hasChangeView = true
        }
    }

    get福袋奖励()

    back2直播界面()

    // 进行点赞
    if (isIn直播界面()) {
        // log('进行点赞')
        // if (!hasExcuteFuncInTime是否在一段时间内执行过('go看直播挂机_点赞')) {
        //     var fordzTimes = getRandomNum(26, 50)
        //     for (var i = 0; i < fordzTimes; i++) {
        //         click(0.5, 0.3, 50)
        //         click(0.5, 0.3, 300)
        //     }
        //     setExcuteFuncInTime一段时间内不再执行('go看直播挂机_点赞', 15 * 60)
        // }
    }

    agentOrHidClick(ocrText('知道了', '下次再说', '我知道', '取消', '点击进入', '进入直播间'))
    // agentOrHidClick(ocrText('取消', '点击进入'))

    enter橱窗()

    if (ocrText('取消关注')) {
        agentOrHidback()
    }

    // 进入购物车浏览商品
    entershopCar购物车()

    // share分享直播间(100)

    write说点什么()

    back2直播界面()

    // sleep.second(300)
    var roundSleep = getRandomNum(30, 180)
    whilestep((steptimes, alltimes, canbreak) => {
        if (ocrText('结束', '下一个')) {
            canbreak && canbreak()
        }
    }, roundSleep, 10)


    agentOrHidback()
    sleep.second(2)
    agentOrHidClick(ocrText('退出'))
}


function go看直播浏览小手柄() {
    log('go看直播浏览小手柄')

    var hasChangeView = false

    log('持续等待直到进入直播间')
    if (ocrText('点击进入', '点击', '下单最高', '最高返', '进入直播')) {
        click(0.5, 0.3)
    }
    if (ocrText('再看一个', '再看一次')) {
        agentOrHidback()
    }

    expectView(() => {
        return isIn直播界面()
    }, (stepTimes) => {
        log('等待中....' + stepTimes)
        sleep.second(1)
    }, () => {
        log('成功进入到直播间')
    }, null, 30)

    print.warn('注意直播任务关注, 亲密度, 购物车(浏览+加购), 分享直播间, 评论')
    // log('倒数5秒后开始执行直播任务')
    // sleep.second(5)

    agentOrHidClick(ocrText('暂不开启', '暂不'))

    // get福袋奖励()

    back2直播界面()

    agentOrHidClick(ocrText('知道了', '下次再说', '我知道', '取消', '点击进入', '进入直播间'))
    // agentOrHidClick(ocrText('取消', '点击进入'))

    if (ocrText('取消关注')) {
        agentOrHidback()
    }

    // 进入购物车浏览商品
    entershopCar购物车()

    agentOrHidback()
    sleep.second(2)
    agentOrHidClick(ocrText('退出'))
}

function closeGGForKs(maxTimes) {
    printinfoOld('closeGGForKs')
    if (maxTimes == undefined) maxTimes = 3;
    // ocrWaitForMoreTextMillisecondInRegion(0, 0, 1, 0.2, 3, '领取成功', '已成功领取奖励', '关闭')
    var closeBtn = ocrWaitForMoreTextMillisecondInRegion(0, 0, 1, 0.2, (maxTimes == 3 ? 30 : 4), '领取成功', '已成功领取奖励', '关闭', '闭')
    if (closeBtn) {
        printinfoOld('找到广告关闭标志位')
        agentOrHidClick(closeBtn, 0)
        sleep.millisecond(500)
        agentClickRight(ocrTextMoreInRegion(0, 0, 1, 0.2, '领取成功', '已成功领取奖励', '关闭', '闭'))

        if (maxTimes == 1) {
            // if (ENABLE_HID_MODE) {
            //     Otg.back() //hid.back()
            // } else {
            //     agentOrHidback()
            // }
            sleep.millisecond(500)
        } else {
            agentClickRight(ocrText('跳过'))
        }
    } else {
        //找图返回目标数组
        var detects = opencv.findImagesEx('图色734823.cv');
        if (detects != null) {
            // printinfoOld(detects);
            agentOrHidClick(detects[0])
            sleep.millisecond(10)
            agentClickRight(detects[0])
            sleep.second(2)
        } else {
            if (ENABLE_BLUE_MODE) {
                hid.back()
            } else {
                if (ENABLE_HID_MODE) {
                    Otg.back() //hid.back()
                } else {
                    agentOrHidback()
                }
            }

            sleep.millisecond(500)
        }
    }

    if (maxTimes <= 0) {
        return
    }


    if (isInGGView() && !ocrText('再看一')) {
        closeGGForKs(maxTimes - 1)
    }
}

function watchKsGG_common(uiObj, skipClick) {
    if (!uiObj) {
        return true
    }

    if (!skipClick) {
        agentOrHidClick(uiObj, 3)
    }

    //判定是否直播广告
    if (ocrWaitForMoreTextMillisecond('更多直播', '送礼得金币', '抽大奖')) {
        // press(0.4, 0.6)
        ocrWaitForText('已领取', 21, 3)

        backUntilFindView(() => {
            return ocrText('领取奖励', '再看一个')
        })

        // agentOrHidClick(ocrText('领取奖励'))
        // press()
        // agentOrHidClickOffectY(ocrWaitForText('不感兴趣'), 100)
        // agentOrHidClick(ocrText('不感兴趣'))
        // sleep.second(2)
        return true
        // return
    }

    //找色返回目标数组
    //找色返回目标数组
    var detects直播 = opencv.findMultiColorEx('图色692973.cv');
    if (detects直播 == null) {
        detects直播 = opencv.findMultiColorEx('图色681698.cv');
    }
    if (detects直播 == null) {
        detects直播 = opencv.findMultiColorEx('图色155044.cv');
    }
    if (detects直播 != null) {
        ocrWaitForText('已领取', 21, 3)

        backUntilFindView(() => {
            return ocrText('领取奖励', '再看一个')
        })

        return true
        // press()
        // agentOrHidClickOffectY(ocrWaitForText('不感兴趣'), 100)
        // agentOrHidClick(ocrText('不感兴趣'))
        // sleep.second(2)
        // return
    }


    // 检测是否在广告页面
    if (!isInGGView()) {
        sleep.second(3)
        // 不是广告页面???--准备再次判定
        if (ocrText('看视频', '日常任务', '点就领', '去赚钱', '抵用金', '任务中心')) {
            return false
        }
    }

    // 找到睡眠时间
    var sleepTime = findSleepTime()
    printinfoOld('找到睡眠时间: ' + sleepTime + '秒')

    whilestep((steptimes, alltiems, canBreak) => {
        agentOrHidClick(ocrText('取消'))
        if (ocrText('再看一', '再看一', '领取成功', '已成功领取奖励')) {
            canBreak()
        }
    }, sleepTime)

    if (ocrText('领取10金币')) {
        setForverExcuteFuncInTime一段时间内不再执行('ks_sj_yc', 7 * 24 * 60 * 60)
    }

    printinfoOld('"等待: 关闭按钮的出现"')
    // ocrText('点击额外', true)
    var djFlag = ocrText('点击额外', '打开并')
    if (!djFlag) {
        //找图返回目标数组
        var djewFlag = opencv.findImagesEx('图色208439.cv')
        if (djewFlag != null) {
            djFlag = djewFlag[0]
        }
    }
    if (djFlag) {
        click(0.5, 0.5)
        agentOrHidClick(djFlag, 1)
        // agentOrHidClick(ocrWaitForMoreTextMillisecond('允许', '打开', '始终'))
        // agentOrHidClick(ocrWaitForMoreTextMillisecondInRegion(0, 0.7, 0.9, 1, 3, '始终允许', '允许'), 1, () => {
        //     agentOrHidClickAbsoultX(ocrTextMoreInRegion(0, 0.7, 1, 1, '取消', '打开'), screen.percentToWidth(0.75))
        // })
        agentOrHidClick(ocrWaitForMoreTextMillisecondInRegion(0, 0.7, 0.9, 1, 3, '始终允许', '允许', '打开'), 1, () => {
            agentOrHidClickAbsoultX(ocrTextMoreInRegion(0, 0.78, 1, 1, '取消', '打开', '仅此一次'), screen.percentToWidth(0.75))
        })
        agentOrHidClick(ocrText('允许', '始终', '打开'))

        // 再次判定
        djFlag = ocrText('点击额外', '打开并')
        if (djFlag) {
            agentOrHidClick(djFlag, 1)
            agentOrHidClick(ocrWaitForMoreTextMillisecondInRegion(0, 0.7, 0.9, 1, 3, '始终允许', '允许', '打开'), 1, () => {
                agentOrHidClickAbsoultX(ocrTextMoreInRegion(0, 0.78, 1, 1, '取消', '打开', '仅此一次'), screen.percentToWidth(0.75))
            })
            agentOrHidClick(ocrText('允许', '始终', '打开'))
        }

        sleep.second(23)
        startApp(currentAppPackName, true)
        sleep.second(1)

        // 再次关闭
        closeGGForKs()
    } else {
        closeGGForKs()
    }

    if (ocrText('继续观看', '再看')) {
        sleep.second(getDectNumber(ocrText('再看')))
        agentOrHidClick(ocrText('继续观看'))
        closeGGForKs()
    }

    if (ocrText('退出', '残忍离开', '先不看', '放弃奖励')) {

        var goOnWatchFlag = ocrTextMoreInRegion(0, 0.5, 1, 0.8, '继续观看', '去完成任务')
        agentOrHidClick(goOnWatchFlag, 1, () => {
            if (goOnWatchFlag && goOnWatchFlag.text().includes('去完成任务')) {
                if (ocrTextInRegion(0.1, 0.6, 0.6, 0.95, '下载并')) {
                    closeGGForKs()
                } else {
                    agentOrHidClick(ocrText('点击额外', '打开并'))
                    click(0.5, 0.5)
                    // agentOrHidClick(ocrWaitForMoreTextMillisecond('允许', '打开', '始终'))
                    // agentOrHidClick(ocrWaitForMoreTextMillisecondInRegion(0, 0.7, 0.9, 1, 3, '始终允许', '允许'), 1, () => {
                    //     agentOrHidClickAbsoultX(ocrTextMoreInRegion(0, 0.7, 1, 1, '取消', '打开'), screen.percentToWidth(0.75))
                    // })
                    agentOrHidClick(ocrWaitForMoreTextMillisecondInRegion(0, 0.7, 0.9, 1, 3, '始终允许', '允许', '打开'), 1, () => {
                        agentOrHidClickAbsoultX(ocrTextMoreInRegion(0, 0.78, 1, 1, '取消', '打开', '仅此一次'), screen.percentToWidth(0.75))
                    })
                    agentOrHidClick(ocrText('允许', '打开', '始终'))

                    sleep.second(23)
                    startApp(currentAppPackName, true)
                    sleep.second(1)
                }
            } else {
                sleepTime = findSleepTime()
                sleep.second(sleepTime)
            }

            // 再次关闭
            closeGGForKs()
        })
    }


    return false

}

function watchKsGG_多次看广告(uiObj, skipClick) {
    if (!uiObj) {
        return
    }


    var is直播广告 = watchKsGG_common(uiObj, skipClick)
    if (is直播广告) {
        agentOrHidClick(ocrText('退出', '残忍离开', '先不看', '放弃奖励', '放弃', '坚持'))
        return
    }


    agentOrHidClick(ocrTextMoreComplete('领取奖励', '再看一次', '再看一个'), 2, () => {
        printinfoOld('再看一次广高')
        if (ocrText('再看一')) {
            //找图返回目标数组
            var detects = opencv.findImagesEx('图色651729.cv');
            if (detects != null) {
                //    printinfoOld(detects);
                agentOrHidClick(detects[0])
                sleep.second(2)
            }
        }

        watchKsGG_多次看广告(uiObj, true)
    })

    // ocrTextTrue('放弃奖励')
    // 强制退出
    agentOrHidClick(ocrText('退出', '残忍离开', '先不看', '放弃奖励', '放弃', '坚持'))
}


function isIncludesImportWord(allStrings) {
    if (allStrings.includes('梦幻')
        // || allStrings.includes('乐鸿捕鱼')
        || allStrings.includes('永恒岛')
        || allStrings.includes('热血')
        // || allStrings.includes('全名江湖')
        || allStrings.includes('三国杀')
        || allStrings.includes('青云决')
        || allStrings.includes('向僵死开炮')
        // || allStrings.includes('剑与远征')
        // || allStrings.includes('大话西游')
        // || allStrings.includes('弹弹堂')
        // || allStrings.includes('野兽领主')
        || allStrings.includes('谋定天下')
        // || allStrings.includes('浮生为歌姬')
        // || allStrings.includes('旭日之城')
        // || allStrings.includes('率土之滨')
        // || allStrings.includes('三国志')
        // || allStrings.includes('征途')
        // || allStrings.includes('完美世界')
        // || allStrings.includes('名扬传奇')
        // || allStrings.includes('上古情歌')
        // || allStrings.includes('伏魔记')
        // || allStrings.includes('万剑至尊')
        // || allStrings.includes('剑与盾')
        // || allStrings.includes('魔镜物语')
        // || allStrings.includes('大三国')
        || allStrings.includes('VIP')
        || allStrings.includes('vip')
        // || allStrings.includes('游卡')
        || allStrings.includes('网易')
        // || allStrings.includes('巨人')
        // || allStrings.includes('世纪')
        // || allStrings.includes('三七')
        // || allStrings.includes('贪玩')
        // || allStrings.includes('米哈')
        // || allStrings.includes('蚁国')
        // || allStrings.includes('车')
        // || allStrings.includes('网易')
    ) {
        return true
    }
    return false
}

function isInGGView() {
    printinfo('判断是否在广告页面')

    var allres = ocrAllRes()
    // 快捷判断快手系是否再广告界面
    if (currentAppPackName == 'com.kuaishou.nebula' || currentAppPackName == 'com.smile.gifmaker') {
        if (ocrTextMoreByRes(allres, '任务中心', '日常任务', '看广告得', '走路赚金币', '邀请好友', '金币购')) {
            return false
        }
    }

    // 适配百度
    if (currentAppPackName == 'com.baidu.searchbox.lite') {
        if (ocrTextMoreByRes(allres, '金币收益', '去提现', '搜索赚', '看文章视频', '现金收益')) {
            return false
        }

        if (ocrTextMoreByRes(allres, '已领取', '已减少')) {
            return true
        }

        //找图返回目标数组
        var detectsclose = opencv.findImagesEx('图色148273.cv');
        if (detectsclose != null) {
            return true
        }
    }

    if (ocrTextMoreCompleteByRes(allres, '广告', '广告1)', '广告|)')) {
        return true
    }

    if (ocrTextMoreCompleteByRes(allres, '反馈', '跳过', '关闭', '广告')) {
        return true
    }

    if (ocrTextMoreByRes(allres, '开心收下', '恭喜你已', '恭喜你获得', '天天预约', '预约领金币', '开宝箱得金币', '现金收益', '金币收益', '福利中心')) {
        return false
    }

    if (ocrTextMoreByRes(allres, '日常福利', '金币红包雨', '立即翻卡', '我知道了')) {
        return false
    }

    if (ocrTextMoreByRes(allres, '立即下载', '应用名称', '开发者', '隐私', '个人信息保护', '可领取奖励', '直播中')) {
        return true
    }

    if (ocrTextMoreByRes(allres, '继续观看', '直播间', '点击进入', '下滑', '记入奖励', '奖励时长', '后可领', '看直播开宝箱')) {
        return true
    }


    //返回
    var detects = opencv.findImagesEx('图色594708.cv');
    if (detects != null) {
        return true
    }

    printinfo('不在广告页面!!!')
    return false

}


function findRealSleepTime(defaultNum) {
    printinfoOld('findRealSleepTime')
    if (defaultNum == undefined) defaultNum = 15
    var findtime = ocrWaitForMoreTextMillisecondInRegion(0, 0, 1, 0.16, 6, '秒', 's后', 's', '后')
    // printinfoOld(findtime)
    if (findtime) {
        var convertTime = getNumber(findtime.text())
        return convertTime
    }

    return defaultNum
}

function findSleepTime(defaultNum) {
    printinfoOld('findSleepTime')
    if (defaultNum == undefined) defaultNum = 45
    var findtime = ocrWaitForMoreTextMillisecondInRegion(0, 0, 1, 0.16, 6, '秒', 's后', 's', '后')
    // printinfoOld(findtime)
    if (findtime) {
        var convertTime = getNumber(findtime.text())
        if (convertTime < 15) {
            return 20
        }
        if (convertTime > 60) {
            return 38
        }
        return convertTime + 5
    }

    return defaultNum
}

// closeGG(1)
function closeGG(maxTimes) {
    // closeGG()
    if (maxTimes == undefined) maxTimes = 3;
    printinfoOld('closeGG: ' + maxTimes)
    // ocrText('领取成功', true)

    // var img = screen.screenShotFull()
    // var ocrres = img.MLKitOcrRect('zh', [0, 0, 1, 0.2]);
    //  printinfoOld(ocrres)
    // printinfoOld(ocrTextMoreInRegion(0, 0, 1, 0.2, '领取成功'))
    // printinfoOld( ocrWaitForMoreTextMillisecondInRegion(0, 0, 1, 0.2, 6, '领取成功', '已成功领取奖励', '关闭'))
    sleep.second(1)
    // 已经在主页面了
    if (ocrText('金币收益', '日常福利', '现金收益', '日常任务', '去提现', '福利中心',)) {
        return
    }

    if (currentAppPackName == 'com.ss.android.article.video') {
        if (ocrText('限时任务', '自动兑换', '赚钱任务')) {
            return
        }
    }

    var closeBtn = ocrWaitForMoreTextMillisecondInRegion(0, 0, 1, 0.2, (maxTimes == 0 ? 10 : 4), '领取成功', '已成功领取奖励', '关闭', '奖励已领取', '完成')
    if (closeBtn) {
        printinfoOld('找到广告关闭标志位')
        sleep.second(Math.floor(Math.random() * 3 + 1))

        agentOrHidClick(closeBtn, 0)
        sleep.millisecond(2000)
        agentClickRight(ocrTextMoreInRegion(0, 0, 1, 0.2, '领取成功', '已成功领取奖励', '关闭', '完成'))

        if (maxTimes == 1) {
            // if (ENABLE_HID_MODE) {
            //     Otg.back() //hid.back()
            // } else {
            //     agentOrHidback()
            // }
            agentClickRight(ocrText('跳过'))
            sleep.second(1)

            if (isInGGView()) {
                printinfo('在广告页进行返回')
                if (ENABLE_BLUE_MODE) {
                    hid.back()
                } else {
                    if (ENABLE_HID_MODE) {
                        Otg.back() //hid.back()
                    } else {
                        agentOrHidback()
                    }
                }

            }
        }
    } else {
        //找图返回目标数组
        var detects = opencv.findImagesEx('图色819156.cv');
        if (detects != null) {
            // printinfoOld(detects);
            agentOrHidClick(detects[0], 0)
            sleep.millisecond(10)
            agentClickRight(detects[0], 2)
        } else {
            if (maxTimes <= 0) {
                agentOrHidClickAbsoultX(ocrText('反馈', '跳过'), screen.percentToWidth(1) - 150)
                return
            }
            if (isInGGView() || isIn直播界面()) {
                printinfo('在广告页或直播页进行返回')
                if (ENABLE_BLUE_MODE) {
                    hid.back()
                } else {
                    if (ENABLE_HID_MODE) {
                        Otg.back() //hid.back()
                    } else {
                        agentOrHidback()
                    }
                }

                sleep.millisecond(500)
                var detectsback = opencv.findImagesEx('图色651156.cv');
                if (detectsback != null) {
                    agentOrHidClick(detectsback[0])
                }

                agentOrHidClick(ocrText('不使用', '坚持退出', '坚决退出', '退出', '放弃'))
            }
        }
    }

    // 适配百度
    if (currentAppPackName == 'com.baidu.searchbox.lite') {

        //找图返回目标数组
        var detectsclose = opencv.findImagesEx('图色148273.cv');
        if (detectsclose != null) {
            agentOrHidClick(detectsclose[0])
        }
    }

    if (maxTimes <= 0) {
        agentOrHidClickAbsoultX(ocrText('反馈', '反', '跳过'), screen.percentToWidth(1) - 150)
        return
    }

    // 勿关闭, 直接跳出
    if (ocrText('先不看', '放弃奖励', '继续观看', '退出', '放弃')) {
        printinfoOld('误关闭, 直接跳出, 去判断继续观看')

        if (ocrText('已获得', '已领取', '领取成功', '恭喜')) {

            return
        } else {
            agentOrHidClick(ocrText('继续', '续观看', '继续观看'), 1, () => {
                sleep.second(findSleepTime(30))
            })
        }
        // return
    }

    if (isInGGView() && !ocrText('再看一', '看视频再', '看广告再')) {
        closeGG(maxTimes - 1)
    }
}



function click_反馈() {

    var isClickfk = false
    var flagfk = ocrWaitForMoreTextMillisecond('反馈', '反', 4)
    agentOrHidClick(flagfk, 2, () => {
        printinfoOld('点击click_反馈!!')
        var flag = ocrText('不感兴趣', '感兴趣', '不感兴', '兴趣')
        if (flag == null) {
            agentClickLeft(flagfk)
            flag = ocrText('不感兴趣', '感兴趣', '不感兴', '兴趣')
        }
        if (flag == null) {
            var ocr1反馈 = opencv.OCREx('图色124335.cv');
            if (ocr1反馈 != null) {
                agentOrHidClick(ocr1反馈.getAllString()[0])
                flag = ocrText('不感兴趣', '感兴趣', '不感兴', '兴趣')
            }
        }
        agentOrHidClick(flag, 1, () => {
            isClickfk = true
        })
    })

    return isClickfk

}



function watchGG_common(uiObj, skipClick, isAgain, go查看详情) {
    if (!uiObj) {
        return false
    }
    printinfoOld('watchGG_common')
    if (!skipClick) {
        printinfoOld('agentOrHidClick -> uiObj')
        agentOrHidClick(uiObj, 3)
    }

    whilestep((a, b, canbreak) => {
        var allres = ocrAllRes()
        if (ocrTextMoreCompleteByRes(allres, '广告', '反馈', '跳过', '立即下载')
            || ocrTextMoreByRes(allres, '秒后', '跳过', '反馈')) {
            canbreak()
        }
    }, 4, 2)

    // 检测是否在广告页面
    printinfoOld('检测是否在广告页面')
    if (!isInGGView()) {
        printinfoOld('isInGGView -> false')
        // 不是广告页面???--准备再次判定
        printinfoOldForce('是广告页面???--准备再次判定', 3)

        if (!ocrText('看视频', '日常任务', '开宝箱', '金币收益', '去提现', '看广告')) {
            sleep.second(2)
            ocrWaitForText('反馈', '跳过', '立即下载', '秒后')
        } else {
            printinfoOldForce('再次点击原来的文案进入视频', 3)
            agentOrHidClick(uiObj, 3)
            var is任然存在 = ocrText('看视频再', '看广告再', '看视频最高', '看广告最高', '看广告领', '看视频领', '看视频翻')
            if (is任然存在 == null) {
                is任然存在 = ocrText('翻倍领取', '看广告翻', '倍领取')
            }
            if (is任然存在) {
                printinfoOld('点击新的视频文案进入视频')
                agentOrHidClickOffectY(is任然存在, 10, 2)
            }
        }

        if (isAgain) {
            printinfoOld('isAgain')
            if (!isInGGView()) {
                printinfoOld('不在广告页面退出')
            }
            sleep.second(8)

            if (!isInGGView()) {
                printinfoOld('不在广告页面退出')
                return false
            }
        } else {
            if (!isInGGView()) {
                printinfoOldForce('不在广告页面退出', 3)
                return false
            }
        }


    }


    // 找到睡眠时间
    var sleepTime = findSleepTime()
    printinfoOld('找到睡眠时间: ' + sleepTime + '秒')
    if (isAgain) {
        if (!ocrText('短剧', '全集', '免费', '剧情', '下一集')) {
            agentOrHidClick(ocrText('继续观看', '续观看', '继续观', '继续'))
        }

    }

    //向下浏览任务
    if (opencv.findImagesEx('图色469135.cv')) {
        whilestep((stepTimes, alltiem, calbreak) => {
            if (stepTimes > 5) {
                if (ocrText('任务已完成')) {
                    calbreak()
                }
            }
            agentSwipDown()
        }, sleepTime)

        // agentOrHidback()
        closeGG()
        return true
    }

    // whilestep((stepTimes, alltimes, canBreak) => {
    //     SystemPrint.log(stepTimes)
    // })

    // printinfoOld('whilestep')

    var hasfeedback = false
    var is古籍广告 = false
    var iswatch详情 = true
    var isHGskip跳过 = false
    var isZbgg = false
    var isBreak = false
    printinfoOld('循环检测古籍, 跳过')
    whilestep((stepTimes, alltimes, canBreak) => {
        // ocrTextTrue('取消')
        // var ocr1取消 = opencv.OCREx('图色274739.cv');
        // if (ocr1取消 != null && ocr1取消.getAllString().includes('消')) {
        //     agentOrHidClick(ocr1取消.getAllDetect()[0])
        // }

        var allres = ocrAllRes()
        if (!hasfeedback && (stepTimes == 1 || stepTimes == 2)) {

            if (!ocrTextMoreByRes(allres, '斗地主', '寻宝', '当皇上', '种树', '快乐学习', '古籍', '芒果', '公益')
                && !ocrTextMoreByRes(allres, '古诗文', '快乐学习', '古籍', '爱心', '短剧', '立即观看', '点击即看', '剧场', '下一集')) {
                printinfoOld('currentAppPackName =>' + currentAppPackName)
                if (ocrTextMoreByRes(allres, '进入直播', '直播间', '更多直播', '直播', '下单', '记入奖励', '奖励时长')) {
                    isZbgg = true
                    iswatch详情 = false
                }

                if (ocrTextMoreByRes(allres, '跳过', '跳')) {
                    printinfoOld('检查到跳过')
                    iswatch详情 = false
                    if (currentAppPackName == 'com.ss.android.ugc.aweme.lite' && CURRENT_OPERATE_DYJSB == '开宝箱' && isEnableSwitch('dj_gj')) {
                        iswatch详情 = false
                        if (!hasExcuteFuncInTime是否在一段时间内执行过('dj_fk')) {
                            setExcuteFuncInTime一段时间内不再执行('dj_fk', 20 * 60)
                            agentOrHidClick(ocrWaitForMoreTextMillisecond('反馈', '反'), 2, () => {
                                hasfeedback = true
                                agentOrHidClick(ocrText('不感兴趣', '感兴趣', '不感兴', '兴趣'))
                            })
                        }
                    }
                    if (currentAppPackName == 'com.ss.android.ugc.aweme.lite' && CURRENT_OPERATE_DYJSB == '红包雨' && isEnableSwitch('dj_hbygj')) {
                        iswatch详情 = false
                        if (!hasExcuteFuncInTime是否在一段时间内执行过('djhby_fk')) {
                            setExcuteFuncInTime一段时间内不再执行('djhby_fk', 20 * 60)
                            hasfeedback = click_反馈()
                        }

                    }

                    if (currentAppPackName == 'com.phoenix.read' && isEnableSwitch('hg_gj')) {
                        iswatch详情 = false
                        isHGskip跳过 = true
                        printinfoOld('红果古籍跳过判断!!!!!!!!!')
                        if (!hasExcuteFuncInTime是否在一段时间内执行过('hg_fk') || getExcuteFuncTimes('hg_fk_times') % 10 == 0) {
                            setExcuteFuncInTime一段时间内不再执行('hg_fk', 10 * 60)
                            addExcuteFuncTimes('hg_fk_times')
                            hasfeedback = click_反馈()
                        }

                    }
                    if ((currentAppPackName == 'com.xs.fm') && isEnableSwitch('ct_gj')) {
                        iswatch详情 = false
                        if (!hasExcuteFuncInTime是否在一段时间内执行过('ct_fk') || getExcuteFuncTimes('ct_fk_times') % 10 == 0) {
                            addExcuteFuncTimes('ct_fk_times')
                            setExcuteFuncInTime一段时间内不再执行('ct_fk', 20 * 60)
                            hasfeedback = click_反馈()
                        }
                    }

                    if ((currentAppPackName == 'com.xs.fm.lite') && isEnableSwitch('ct_gj') && !isEnableSwitch('gg_force_ctyyb')) {
                        iswatch详情 = false
                        if (!hasExcuteFuncInTime是否在一段时间内执行过('ctyy_fk') || getExcuteFuncTimes('ctyy_fk_times') % 10 == 0) {
                            addExcuteFuncTimes('ctyy_fk_times')
                            setExcuteFuncInTime一段时间内不再执行('ctyy_fk', 20 * 60)
                            hasfeedback = click_反馈()
                        }
                    }

                    if ((currentAppPackName == 'com.ss.android.article.video') && isEnableSwitch('xgsp_gj')) {
                        iswatch详情 = false
                        if (!hasExcuteFuncInTime是否在一段时间内执行过('xgsp_fk') || getExcuteFuncTimes('xgsp_fk_times') % 10 == 0) {
                            addExcuteFuncTimes('xgsp_fk_times')
                            setExcuteFuncInTime一段时间内不再执行('xgsp_fk', 20 * 60)
                            hasfeedback = click_反馈()
                        }

                    }
                } else {
                    if (currentAppPackName == 'com.ss.android.ugc.aweme.lite' && CURRENT_OPERATE_DYJSB == '开宝箱' && isEnableSwitch('dj_gj')) {
                        hasfeedback = click_反馈()
                        iswatch详情 = false
                    }
                    if (currentAppPackName == 'com.ss.android.ugc.aweme.lite' && CURRENT_OPERATE_DYJSB == '红包雨' && isEnableSwitch('dj_hbygj')) {
                        hasfeedback = click_反馈()
                        iswatch详情 = false
                    }
                    if (currentAppPackName == 'com.phoenix.read' && isEnableSwitch('hg_gj')) {
                        hasfeedback = click_反馈()
                        iswatch详情 = false
                        agentOrHidback()
                        closeGG()
                        agentOrHidClick(ocrWaitForText('坚持退出', '退出', '放弃'))
                        if (isInGGView()) {
                            var detects = opencv.findImagesEx('图色819156.cv');
                            if (detects != null) {
                                agentOrHidClick(detects[0])
                                agentOrHidClick(ocrWaitForText('坚持退出', '退出', '放弃'))

                                canBreak()
                            }
                        }
                    }
                    if ((currentAppPackName == 'com.xs.fm' || currentAppPackName == 'com.xs.fm.lite') && isEnableSwitch('ct_gj')) {
                        hasfeedback = click_反馈()
                        iswatch详情 = false
                    }
                }


            } else {
                is古籍广告 = true
            }
        }

        if (ocrTextMoreByRes(allres, '金币收益', '现金收益', '福利中心', '日常福利', '去提现', '自动兑换')) {
            canBreak()
            isBreak = true
        }

        if (is古籍广告 && currentAppPackName == 'com.woodleaves.read') {
            is_gj_gg = true
        }

        if ('com.ss.android.article.video' == currentAppPackName && is古籍广告) {
            if (getExcuteFuncTimes('gjgg_xgsp_times') > 2) {
                // 都是古籍广告就不管了
            } else {
                canBreak()
                isBreak = true
            }

            addExcuteFuncTimes('gjgg_xgsp_times')
        }

        agentClickRightOffectRight(ocrTextMoreByRes(allres, '取消'), 10, 1, () => {
            if (ocrWaitForMoreTextMillisecond('更多直播', '说点什么', '人气', '小时榜', 4)) {
                agentOrHidback()
                sleep.second(1)
            }
            if (!ocrText('领取成功', '后可', '获取奖励', '领取奖励', '秒后', '获取奖励 ')) {
                agentOrHidback()
                sleep.second(1)
            }

            agentOrHidClick(ocrText('继续'))
        })

        agentOrHidClickOffectY(ocrTextMoreByRes(allres, '鲜花'), 50)

        // 判断是否广告已经结束
        if (sleepTime > 25 && alltimes > 20) {
            if (ocrTextMoreByRes(allres, '已领取', '再看一', '关闭', '开心收下', '领取成功', '已成功领取奖励')) {
                canBreak()
            }
        }

        //找图返回目标数组
        var detects弹窗 = opencv.findImagesEx('图色585677.cv');
        if (detects弹窗 != null) {
            //x
            var detectsCLOSE = opencv.findImagesEx('图色540246.cv');
            if (detectsCLOSE != null) {
                agentOrHidClick(detectsCLOSE[0], 0)
            }
            // agentOrHidClickAbsoultX(detects[0], screen.percentToWidth(0.78))
        }



    }, sleepTime)

    if ('com.ss.android.article.video' == currentAppPackName) {
        if (is古籍广告 && isBreak) {
            closeGG()
            agentOrHidClick(ocrText('坚持退出'))
            return false
        } else {
            addExcuteFuncTimes('gjgg_xgsp_times', 0)
        }
    }

    if (isHGskip跳过) {
        sleep.second(findRealSleepTime(2))
        ocrWaitForText('去提现', '开宝箱', '日常福利', '新人福利', 3)
        agentOrHidClick(ocrText('领取成功', '已成功领取奖励', '关闭', '奖励已领取', '闭', '跳过'))
        // closeGG(2)
        agentOrHidClickAbsoultX(ocrText('反馈', '反', '跳过'), screen.percentToWidth(1) - 150)
        agentOrHidClick(ocrText('坚持退出', '退出', '放弃'))
        printinfoOld('红果跳过自然流逝')
        return false
    }

    if (is古籍广告) {
        printinfoOld('古籍广告')
        sleep.second(2)
        closeGG()
        agentOrHidClick(ocrWaitForText('领取成功', '已成功领取奖励', '关闭', '奖励已领取', '闭', '跳过'))
        agentOrHidClick(ocrText('坚持退出', '退出', '放弃'))

        if (ocrText('反馈', '关闭', '跳过', '闭', '领取成功')) {
            agentOrHidback()
            sleep.second(1)
            if (ocrText('反馈', '关闭', '跳过', '闭', '领取成功')) {
                agentOrHidClick(ocrText('关闭', '跳过', '闭', '领取成功'))
            }
        }
        return false
    }

    if (!iswatch详情) {
        // 如果是直播广告, 不看详情
        if (isZbgg) {
            printinfoOld('直播广告')

            if (['com.ss.android.ugc.aweme.lite'].includes(currentAppPackName)) {
                if (!hasExcuteFunc('养宝箱_times_DYJSB')
                    && isEnableSwitch('dj_auto_zb')
                    && (isBetweenTime(0, 2) || isBetweenTime(20, 24))) { // && isBetweenTime(0, 9)

                    startThredTaskMaxTime超时结束(() => {
                        go看直播挂机()
                    }, 15 * 60)

                    setExcuteFunc('养宝箱_times_DYJSB')
                }
            }

            var closeFlag = ocrWaitForText('领取成功', '已成功领取奖励', '关闭', '奖励已领取', '闭', '跳过')
            if (currentAppPackName == 'com.xs.fm') {
                agentOrHidback()
                sleep.second(1)
            } else {
                agentOrHidClick(closeFlag)
            }

            if (isIn直播界面_sample()) {
                // 关闭直播
                var detects关闭直播 = opencv.findImagesEx('图色199710.cv');
                if (detects关闭直播 != null) {
                    agentOrHidClick(detects关闭直播[0])
                }

                agentOrHidClick(ocrWaitForText('领取成功', '已成功领取奖励', '关闭', '奖励已领取', '闭', '跳过'))
                agentOrHidClick(ocrText('不使用', '坚决退出', '退出'))

                if (isIn直播界面_sample()) {
                    closeGG()
                }
            }

            sleep.second(1)

            if (ocrText('反馈', '关闭', '跳过', '领取成功', '直播间观', '记入奖励', '取成功')) {
                agentOrHidback()
                sleep.second(1)

                var exitrepert = ocrText('反馈', '关闭', '跳过', '领取成功', '直播间观', '记入奖励', '取成功')
                if (exitrepert) {
                    agentClickRightOffectRight(exitrepert, 10)
                }
            }

            return true
        }
        sleep.second(2)
        closeGG()
        agentOrHidClick(ocrWaitForText('领取成功', '已成功领取奖励', '关闭', '奖励已领取', '闭', '跳过'))
        if (ocrText('反馈', '跳过', '闭', '领取成功', '个人信息')) {
            agentOrHidback()
            sleep.second(1)
            if (ocrText('反馈', '跳过', '闭', '领取成功', '个人信息')) {
                agentOrHidClick(ocrText('关闭', '跳过', '闭', '领取成功'))
            }
        }
        return true
    }


    if (!hasfeedback) {
        //  判定是否直播广告
        if (ocrText('进入直播', '直播间', '更多直播', '直播', '下单')) {
            printinfoOld('判定是否直播广告')
            printinfoOld('5%几率进入看直播')


            if (Math.random() * 100 > 30) {

                if (!uiSwitch.findByID(控件ID = "watchzb").getOn()) {
                    agentOrHidClick(ocrText('进入直播', '直播间'))
                    sleep.second(10)
                    go看直播详情(30)

                    // 关闭直播
                    var detects关闭直播 = opencv.findImagesEx('图色199710.cv');
                    if (detects关闭直播 != null) {
                        agentOrHidClick(detects关闭直播[0])
                    }

                    agentOrHidClick(ocrText('不使用', '坚决退出', '退出'))
                }
            }
        } else {

            var allres = ocrAllRes()
            if (go查看详情 && (!ocrTextMoreByRes(allres, '斗地主', '寻宝记', '当皇上', '种树', '关闭', '直播', '竖屏剧场', '精彩', '跳过')
                || isIncludesImportWord(allres.getAllString()))
            ) {
                printinfoOld('75%几率查看详情')
                var flag详情 = ocrTextMoreByRes(allres, '查看详情', '下载')
                if (flag详情 == null) {
                    //找图返回目标数组
                    var finddects = opencv.findImagesEx('图色832228.cv');
                    if (finddects != null) {
                        flag详情 = finddects[0]
                    }
                }

                if (flag详情) {
                    agentOrHidClickOffectY(flag详情, 200, 3, () => {
                        reperatAgentSwipSmallDown循环向下浏览(false, 6, 5)

                        //找图返回目标数组
                        var detectsback = opencv.findImagesEx('图色651156.cv');
                        if (detectsback != null) {
                            agentOrHidClick(detectsback[0])

                            detectsback = opencv.findImagesEx('图色651156.cv');
                            if (detectsback != null) {
                                //找图返回目标数组
                                var detectsclose详情 = opencv.findImagesEx('图色662363.cv');
                                if (detectsclose详情 != null) {
                                    agentOrHidClick(detectsclose详情[0])
                                }
                            }
                        } else {
                            agentOrHidback()
                        }


                    })
                }

            }

            sleep.second(Math.floor(Math.random() * 5))
        }
    }

    if (isIn直播界面()) {
        printinfoOld('还在直播界面, 退出')
        var detects关闭直播 = opencv.findImagesEx('图色199710.cv');
        if (detects关闭直播 != null) {
            agentOrHidClick(detects关闭直播[0])
        }
        agentOrHidClick(ocrText('不使用', '坚决退出', '退出'))

        if (isIn直播界面()) {
            printinfoOld('再次判断还在直播界面, 退出')
            var detects关闭直播 = opencv.findImagesEx('图色199710.cv');
            if (detects关闭直播 != null) {
                agentOrHidClick(detects关闭直播[0])
            }
            agentOrHidClick(ocrText('不使用', '坚决退出', '退出'))
        }
    }

    printinfoOld('"等待: 关闭按钮的出现"')

    closeGG()

    printinfoOld('判断是否继续观看')
    if (ocrText('先不看', '放弃奖励', '继续观看', '坚持退出')) {

        var goOnWatchFlag = ocrTextMoreInRegion(0, 0.3, 1, 0.8, '继续观看', '去完成任务', '继续')
        agentOrHidClick(goOnWatchFlag, 1, () => {
            sleepTime = findSleepTime()
            sleep.second(sleepTime)
            ocrWaitForMoreTextMillisecond('领取成功', '已成功领取奖励', '关闭', '奖励已领取', '闭', 30)

            // 再次关闭
            closeGG()
        })
    }

    printinfoOld('watchGG_common_end')
    return true
}

function watchGG_aginCallback_多次看广告(uiObj, againCallBackContine, maxTimes, skipClick) {
    watchGG_多次看广告(uiObj, skipClick, false, maxTimes, againCallBackContine)
}

function watchGG_多次看广告(uiObj, skipClick, isAgain, maxTimes, againCallBack) {
    if (!uiObj) {
        return
    }
    if (maxTimes == undefined) {
        maxTimes = 6;
        if (currentAppPackName == 'com.ss.android.ugc.aweme.lite'
            || currentAppPackName == 'com.ss.android.ugc.live') {
            maxTimes = 4
        }
        if (currentAppPackName == 'com.ss.android.article.search') {
            maxTimes = 8
        }
        if (currentAppPackName == 'com.ss.android.article.lite') {
            maxTimes = 8
        }
    }



    if (isAgain == undefined) isAgain = false

    var isGoDetail = Math.random() * 100 > 75
    if (currentAppPackName == 'com.baidu.searchbox.lite') {
        isGoDetail = false
    }
    if (!watchGG_common(uiObj, skipClick, isAgain, isGoDetail)) {
        // 强制退出
        log('强制退出')
        agentOrHidClick(ocrText('退出', '残忍离开', '先不看', '坚持', '放弃', '强制'))
        return
    }

    var allres = ocrAllRes()
    if (maxTimes <= 0) {
        agentOrHidClick(ocrTextMoreByRes(allres, '退出', '残忍离开', '先不看', '坚持', '放弃', '强制'))
        return
    }

    if (ocrTextMoreByRes(allres, '评价并收下', '开心收下', '去提现', '福利中心', '日常福利', '金币收益', '现金收益')) {
        log('已经结束看广告了!')
        return
    }

    if (ocrTextMoreByRes(allres, '如有疑问', '活动规则', '听书赚金币', '阅读赚金币', '吃饭补贴', '看剧赚钱', '金币红包雨')) {
        log('已经结束看广告了!')
        return
    }

    if (currentAppPackName == 'com.xs.fm') {
        if (ocrTextMoreByRes(allres, '金币红包雨', '收益日报', '开宝箱得', '听歌赚钱', '睡觉赚', '吃饭赚')) {
            log('已经结束看广告了!')
            return
        }

    }



    if (['com.phoenix.read', 'com.ss.android.ugc.aweme.lite'].includes(currentAppPackName)) {
        printinfoOld('去判断再看一次广告')

        if (againCallBack && !againCallBack()) {
            return
        }
        // ocrText('领取奖励', true)
        var flag领取奖励
        var ocr1 = opencv.OCREx('图色222081.cv');
        if (ocr1 != null && ocr1.getAllString() != null) {
            if (['领取奖励', '领取奖', '取奖励', '领奖励'].includes(ocr1.getAllString())) {
                flag领取奖励 = ocr1.getAllDetect()[0]
            } else {
                flag领取奖励 = ocrTextMoreComplete('领取奖励', '再看一次', '再看一个')
            }
        } else {
            flag领取奖励 = ocrTextMoreComplete('领取奖励', '再看一次', '再看一个')
        }

        if (currentAppPackName == 'com.ss.android.ugc.aweme.lite') {
            currentAppPackName = 'com.ss.android.ugc.aweme.lite'
            var flag定位 = ocrText('再看一', '再看一个', '额外获得')
            if (flag定位) {
                var reactkgg = flag定位.getRect()
                var jinbFlag = getMaxNumber(ocrGetAllStrInRegion(screen.widthToPercent(reactkgg.getLeft() - 100),
                    screen.heightToPercent(reactkgg.getTop()),
                    reactkgg.getRight() + 100,
                    screen.heightToPercent(reactkgg.getTop() + 500)))

                if (jinbFlag > 0 && jinbFlag < 660) {
                    flag领取奖励 = null
                }
            }
        }
        // agentOrHidClick(flag领取奖励)
        agentOrHidClick(flag领取奖励, 2, () => {

            // var moreFlag = ocrText('看视频再', '看广告')
            // if (getDectNumber(moreFlag) >= 10 && getDectNumber(moreFlag) < 1450) {
            //     printinfoOld('再看一次广告金币数量偏低, 跳过')
            //     return
            // }

            var flag = ocrText('再看一')
            if (flag) {
                //找图返回目标数组
                var detects = opencv.findImagesEx('图色651729.cv');
                if (detects != null) {
                    //    printinfoOld(detects);
                    agentOrHidClick(detects[0])
                    sleep.second(2)
                }

                flag = ocrText('再看一')
                if (flag) {
                    agentOrHidClick(ocrText('领取奖励'))
                }
            }

            watchGG_多次看广告(uiObj, true, true, maxTimes - 1, againCallBack)
            return
        })
    }


    printinfoOld('判断再看一次广告')
    // ocrText('再看一次', true)
    var moreFlagAgain = ocrText('看视频再', '看广告再', '再看一')
    // if (getDectNumber(moreFlagAgain) <= 150 && getDectNumber(moreFlagAgain) > 1) {
    //     if (currentAppPackName == 'com.cat.readall') {
    //         printinfoOld('悟空金币达到上限')
    //         
    //         setExcuteFuncInTime一段时间内不再执行('start_收集金币_WKLLQ', 24 * 60 * 60)
    //         sleep.second(5)
    //         
    //     }
    // }

    if (againCallBack) {
        if (!againCallBack()) {
            return
        }

        agentOrHidClick(moreFlagAgain, 1, () => {
            if (ocrText('再看一')) {
                agentOrHidClick(ocrText('领取奖励'))
            }
        })
        watchGG_多次看广告(moreFlagAgain, true, true, maxTimes - 1, againCallBack)
    } else {

        // SystemPrint.log(getDectNumber(moreFlagAgain))
        // SystemPrint.log(currentAppPackName)
        var flag = ocrText('+', '金币', '领取奖励', '再看一')
        var jinstr = ocrGetAllStrInNearDect(flag, 250, 200)

        printinfo(`获取内容: ${jinstr}`)
        if ((getMaxNumByStr(jinstr) > 1 && getMaxNumByStr(jinstr) < 2000) || getMaxNumByStr(jinstr) > 10000) {

            if ((getDectNumber(moreFlagAgain) >= 1200 || isEnableSwitch('gg_force'))
                && (currentAppPackName == 'com.ss.android.article.search' || currentAppPackName == 'com.luna.music')) {
                watchGG_多次看广告(moreFlagAgain, false, true, maxTimes - 1)
                return
            }

            if (getDectNumber(moreFlagAgain) > 660 && currentAppPackName == 'com.ss.android.ugc.aweme.lite') {
                watchGG_多次看广告(moreFlagAgain, false, true, maxTimes - 1)
                return
            }

            // 百度极速版
            if (currentAppPackName == 'com.baidu.searchbox.lite') {
                if (getDectNumber(moreFlagAgain) > 198) {
                    watchGG_多次看广告(moreFlagAgain, false, true, maxTimes - 1)
                    return
                } else {
                    agentOrHidClick(ocrText('退出', '残忍离开', '先不看', '坚持', '放弃'))
                    return
                }
            }

        } else {
            agentOrHidClick(moreFlagAgain, 1, () => {
                if (ocrText('再看一')) {
                    agentOrHidClick(ocrText('领取奖励'))
                }
            })
            watchGG_多次看广告(moreFlagAgain, true, true, maxTimes - 1)
            return
        }
    }

    printinfoOld('closeGG(1)')
    closeGG(1)

    // 强制退出
    printinfoOld('强制退出')
    agentOrHidClick(ocrText('退出', '残忍离开', '先不看', '坚持', '放弃'))

    if (isInGGView()) {
        printinfoOld('二次强制退出')
        closeGG(1)
        agentOrHidClick(ocrText('退出', '残忍离开', '先不看', '坚持', '放弃'))
    }
}

function watchGG_单次模式(uiObj) {
    if (!uiObj) {
        return
    }

    watchGG_common(uiObj, false, false, getRandomNum(0, 100) > 35)

    // 强制退出
    agentOrHidClick(ocrWaitForMoreTextMillisecond('坚持退出', '退出', '放弃'))
}



function watchGG_简单模式(uiObj, skipClick) {
    if (!uiObj) {
        return
    }

    if (skipClick == undefined) {
        skipClick = false
    }

    watchGG_common(uiObj, skipClick, false, false)

    // 强制退出
    agentOrHidClick(ocrWaitForMoreTextMillisecond('坚持退出', '退出', '放弃', '完成任务'))
}



function watchGG_简单模式_看广告详情(uiObj) {
    if (!uiObj) {
        return
    }

    watchGG_common(uiObj, false, false, true)

    // 强制退出
    agentOrHidClick(ocrWaitForMoreTextMillisecond('坚持退出', '退出', '放弃'))
}
